// const { default: axios } = require("axios");
const { list } = require("./mock");

module.exports = (app) => {
  app.get("/api/list", (req, res) => {
    // ["诚信818", "会员商家", "称新车", "限时特惠", "视频看车"]

    // desc = a  默认排序
    // desc = b  最新发布
    // desc = c  价钱降序

    const {
      productType = "全部",
      priceType = "a",
      type = "a",
      desc = "a",
      pageCode = 1,
      pageSize = 10,
    } = req.query;

    const result = list
      .filter((v) => {
        if (productType === "全部") return true;
        return v.productType.includes(productType);
      })
      .filter((v) => {
        // priceType = a  全部
        // priceType = b  3-10
        // priceType = c  10-30
        // priceType = d  30-100
        // priceType = e  100-150
        // priceType = f  150-200
        switch (priceType) {
          case "a":
            return true;
          case "b":
            return 3 <= v.price && v.price < 10;
          case "c":
            return 10 <= v.price && v.price < 30;
          case "d":
            return 30 <= v.price && v.price < 100;
          case "e":
            return 100 <= v.price && v.price < 150;
          case "f":
            return 150 <= v.price && v.price <= 200;
        }
      })
      .filter((v) => {
        // type = a  全部
        // type = b  诚信818
        // type = c  会员商家
        // type = d  称新车
        // type = e  限时特惠
        // type = f  视频看车
        switch (type) {
          case "a":
            return true;
          case "b":
            return v.type === "诚信818";
          case "c":
            return v.type === "会员商家";
          case "d":
            return v.type === "称新车";
          case "e":
            return v.type === "限时特惠";
          case "f":
            return v.type === "视频看车";
        }
      })
      .sort((a, b) => {
        // desc = a  默认排序
        // desc = b  最新发布
        // desc = c  价钱降序
        if (desc === "a") return;
        if (desc === "b") return new Date(b.date) - new Date(a.date);
        if (desc === "c") return b.price - a.price;
      });

    res.send({
      code: 200,
      data: result.slice((pageCode - 1) * pageSize, pageCode * pageSize),
      total: result.length,
    });
  });

  // axios.get('/api/detail/12312312313211')
  app.get('/api/detail/:id', (req, res) => {
    const { id } = req.params
    const result =list.find(v => v.id === id);
    res.send({
      code: 200,
      html: `
        <div style="width: 800px;margin: 0 auto;">
          <dl style="display: flex;">
            <dt>
              <img src="${result.url}" style="width: 400px;"/>
            </dt>
            <dd>
              <h3>${result.productType}</h3>
              <h3>${result.price}</h3>
              <h3>${result.date}</h3>
            </dd>
          </dl>
        </div>
      `
    })
  })

  // axios.get("/api/detail", { params: { id: 1231231231 } });
  // app.get("/api/detail", (req, res) => {
  //   const { id } = req.query;
  // });

  // axios.post("/api/detail",  { id: 1231231231 } );
  // app.post("/api/detail", (req, res) => {
  //   const { id } = req.body;
  // });
};
